import asyncio
import json
import time

from curl_cffi import requests
import subprocess
from functools import partial

subprocess.Popen = partial(subprocess.Popen, encoding="utf-8")
import execjs
import hashlib

with open("h5st514.js", "r", encoding="utf-8") as f:
    js = f.read()

js_com = execjs.compile(js)


def sha_256(message):
    return hashlib.sha256(message.encode("utf-8")).hexdigest()


def convert_cookie_dict_to_string(cookie_dict):
    # Join each key-value pair with '=' and then join all pairs with '; '
    cookie_string = "; ".join([f"{key}={value}" for key, value in cookie_dict.items()])
    return cookie_string


url = "https://api.m.jd.com/"


def generate_h5st(params):
    # 解析原始 body
    body_str = params["body"]
    body = json.loads(body_str) if isinstance(body_str, str) else body_str

    # 创建用于 SHA256 加密的 boddy
    boddy = {
        "tenantCode": body["tenantCode"],
        "bizModelCode": body["bizModelCode"],
        "bizModeClientType": body["bizModeClientType"],
        "externalLoginType": body["externalLoginType"],
        "key": body["key"],
        "datatype": body["datatype"],
        "page": body["page"],
        "pagesize": body["pagesize"],
        "ext_attr": body["ext_attr"],
        "brand_col": body["brand_col"],
        "price_col": body["price_col"],
        "color_col": body["color_col"],
        "size_col": body["size_col"],
        "ext_attr_sort": body["ext_attr_sort"],
        "merge_sku": body["merge_sku"],
        "multi_suppliers": body["multi_suppliers"],
        "area_ids": body["area_ids"],
        "filt_type": body["filt_type"],
        "qp_disable": body["qp_disable"],
        "debug": body["debug"],
        "t1": body["t1"],
        "sourceType": body["sourceType"],
    }

    # 创建用于生成 h5st 的 a 对象
    a = {
        "appid": params.get("appid", "search-pc-java"),
        "body": sha_256(json.dumps(boddy, separators=(",", ":"))),
        "clientVersion": params.get("clientVersion", "1.0.0"),
        "client": params.get("client", "pc"),
        "functionId": params.get("functionId", "pc_search_s_new"),
        "t": int(time.time() * 1000),  # 使用当前时间戳
    }

    # 调用 JS 的 PSign.sign 方法生成 h5st
    h5st = js_com.call("getH5st", a)

    return h5st


headers = {
    "accept": "application/json",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,ar;q=0.7",
    "cache-control": "no-cache",
    "origin": "https://so.m.jd.com",
    "pragma": "no-cache",
    "priority": "u=1, i",
    "referer": "https://so.m.jd.com/",
    "sec-ch-ua": '"Not)A;Brand";v="8", "Chromium";v="138", "Microsoft Edge";v="138"',
    "sec-ch-ua-mobile": "?1",
    "sec-ch-ua-platform": '"Android"',
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Mobile Safari/537.36 Edg/138.0.0.0",
    "x-referer-page": "https://so.m.jd.com/ware/search.action",
    "x-rp-client": "h5_1.0.0",
}
cookies = {
    "b_webp": "1",
    "b_avif": "1",
    "jcap_dvzw_fp": "_HIVLiZCIEZDIcHwXjPU43al5uvsYIwPfs4O6VE9nqABptbi3ASG0seWjzU_o2F4bpTtKrH1VphdN9RwziykSA==",
    "whwswswws": "",
    "__jdv": "122270672%7Cdirect%7C-%7Cnone%7C-%7C1751545604909",
    "__jdu": "1751545604909534518369",
    "TrackID": "1io42MDGljZIxKh9nQPT2pDURlFZggkjYvKS71DQbrlTvaOy5RsJZMvGkwdJYjk69Iudaku891qUXOx38zcS0v4514tMicU7e9U2MUQo_Kcck89R-Zvnp0SET6Py7jWmG",
    "light_key": "AASBKE7rOxgWQziEhC_QY6yavDj_xHhkMKVr8vjDB9dBA0T7H7GnmycRgYbZUdKPMeQWX75-",
    "pinId": "briEibRb-h1YgxkG81uaz7V9-x-f3wj7",
    "pin": "jd_7eac037a858fc",
    "unick": "jd_8js7q5tn2s7n2h",
    "_tp": "aO5XtogPiplRTZPyN6P5Vkd5JC%2FQJe7kEmh0g0oTaNI%3D",
    "_pst": "jd_7eac037a858fc",
    "ipLoc-djd": "19-1607-0-0",
    "shshshfpa": "ab96e4b0-82d6-a907-9206-6c096fe807bb-1751545628",
    "shshshfpx": "ab96e4b0-82d6-a907-9206-6c096fe807bb-1751545628",
    "areaId": "19",
    "mail_times": "2%2C1",
    "PCSYCityID": "CN_440000_440300_0",
    "__jdc": "95931165",
    "wlfstk_smdl": "rar4lhajsosut2ijpchtgjijafkyl2ic",
    "3AB9D23F7A4B3C9B": "GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBE",
    "wxa_level": "1",
    "cid": "9",
    "jxsid": "17522244011849222518",
    "appCode": "ms0ca95114",
    "webp": "1",
    "visitkey": "5225087356839826126",
    "mba_muid": "1751545604909534518369",
    "cd_eid": "jdd03GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBEAAAAMX7C2NSPQAAAAAC7YKOXOEBJ37QAX",
    "sbx_hot_h": "null",
    "autoOpenApp_downCloseDate__jd_czlogin_openapp_": "1752224419174_1",
    "b_dw": "1912",
    "b_dh": "954",
    "b_dpr": "1",
    "bf_hybrid": "d_-",
    "shshshfpb": "BApXSZuC2-_JAYEqgcJFHsyDPBX7ULrZhBhMVUQdq9xJ1MppMe462",
    "TrackerID": "oAk76mdodKzBwUzsurOeo1UbehD1abdVOCEYZt1g_UG9EboR7O-lCWIynQeXdSgibmi-N-nd5ziXFLef0Emzp8OjTHerZLjkughDpyuVaKHDq262EJYGQFcV7ZEdgIPO",
    "pt_key": "AAJocNsmADCZKChm7vBEQs4QxyZFvoNoZ2_1H3-bWCItrCDSSMziykVYJ4oxftE7FCsQ-YqyerU",
    "pt_pin": "jd_7eac037a858fc",
    "pt_token": "yasozpru",
    "pwdt_id": "jd_7eac037a858fc",
    "sfstoken": "tk01mb08e1cbfa8sMysxK1NmMXZ5/FhE4HYdwBQaoBFI0NlX1WYqcGIj9UXazy9ebxY2FqlPhIhlXgjbkxuX9/kTZMeO",
    "qid_uid": "f6952c8d-4d1d-4b40-952c-25d751dd14e8",
    "qid_fs": "1752226601966",
    "qid_ls": "1752226601966",
    "qid_ts": "1752226601974",
    "qid_vis": "1",
    "qid_sid": "f6952c8d-4d1d-4b40-952c-25d751dd14e8-1",
    "p-request-id": "jd_7eac037a858fc2025071117ViqKhcNtvS",
    "qid_seq": "3",
    "qid_evord": "6",
    "autoOpenApp_downCloseDate_jd_homePage": "1752226834147_1",
    "autoOpenApp_downCloseDate_auto": "1752226845692_1800000",
    "e_wq_addr": "CMU3GzPpDzTpDJU2DzHpCMU3GyV1DJCnDyV1DOVLG18vdJY3CUGvdJu2CzCvdJUzC0PpTXU5HUO2TXU1GtUmTXU1HJu3TXU4ENU3TXU5CNUzXyU3GyV1DJCnDyV1DOVLGyV1DUUmCsV1DtcnHMV1EJYzCyV1DJCzGIV1EUVLDsV1DUS1CMV1DUU5DyV1ENq1DyV1EJK1CyV1DtcnHMV1EJYzCyV1DJO2GyV1DJZPHMU3GzOnDs40ENSyDzYvCuCzEG==",
    "wq_addr": "0%7C1_72_55674_0%7C%u5317%u4EAC_%u671D%u9633%u533A_%u9EA6%u5B50%u5E97%u8857%u9053_%7C%u5317%u4EAC%u5E02%u671D%u9633%u533A%u9EA6%u5B50%u5E97%u8857%u9053%u671D%u9633%u516C%u56ED%7C116.482276%2C39",
    "jdAddrId": "1_72_55674_0",
    "jdAddrName": "%u5317%u4EAC_%u671D%u9633%u533A_%u9EA6%u5B50%u5E97%u8857%u9053_",
    "commonAddress": "0",
    "regionAddress": "1%2C72%2C55674%2C0",
    "mitemAddrId": "1_72_55674_0",
    "mitemAddrName": "%u5317%u4EAC%u5E02%u671D%u9633%u533A%u9EA6%u5B50%u5E97%u8857%u9053%u671D%u9633%u516C%u56ED",
    "__jda": "95931165.1751545604909534518369.1751545604.1752224288.1752226846.4",
    "sc_width": "904",
    "mt_xid": "V2_52007VwMUVl1cV14YSBxUBG8HEFdZWFtYGEEpXVBjCkcCCAhOWBsdGUAAZQAUTg1eVFMDHhoLDDdQEQBeXFBdL0oYXwF7AhROXVhDWhlCHFQOZwMiUG1YYlMfSB1eBmMEFFJcX1ZZGk4RWQ1XAxFRXw%3D%3D",
    "pt_st": "1_6QzL9UmPa5r8oX-Oxh6Wn2SUv9jw7haZkPF-PQqT0UNgvqzl0w0f-fsYXhTfJJojCz3AhssyUuYj9Aog6OtQjSRUlC2vdF5e7ZStjnIMG8lHuoudz_05HzxlnY-lFQkA2hfWU_u74TdTWZjHm6GVTD7oodqel2ek6XovEGknIspNyJUswM3r_Wf2s3vObKvsuJfeb_4QCEBntLroJhGK_dPxKPb0dgHMT9RYhON4",
    "retina": "1",
    "warehistory": '"100124552423,10092745719292,100124552423,100126228944,"',
    "3AB9D23F7A4B3CSS": "jdd03GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBEAAAAMX7DVLJKIAAAAACB27ZOGOS6VJYMX",
    "autoOpenApp_downCloseDate_autoOpenApp_autoPromptly": "1752227819923_1",
    "__wga": "1752227820178.1752227355162.1752227355162.1752227355162.4.1",
    "PPRD_P": "UUID.1751545604909534518369-LOGID.1752227820184.1915978517",
    "wqmnx1": "MDEyNjM2MnRzZHdlYT9yJSUlJSUlJSZocyYmc1Q1X3MxNTY2ejVpQWQgIGw1cGIzKCwgKW0uIGVyLmcwNGY0ZkJLWUNGRigl",
    "__jdb": "95931165.33.1751545604909534518369|4.1752226846",
    "mba_sid": "17522244014911954953588.30",
    "__jd_ref_cls": "MYL_Display_Init",
    "jxsid_s_t": "1752227824954",
    "jxsid_s_u": "https%3A//so.m.jd.com/ware/search.action",
    "sdtoken": "AAbEsBpEIOVjqTAKCQtvQu17clXefttGMQKKhY56CPcDx_T4Eh0YayFXXeVQaRksCneaBolfxSWvThhPam5YP7uj_Uz-5QoytzGB3dLodE96yKFqaceLo3cj8pcN",
}
url = "https://api.m.jd.com/api"
params = {
    "functionId": "searchKeyword",
    "appid": "jd-cphdeveloper-m",
    "body": '{"tenantCode":"jgm","bizModelCode":5,"bizModeClientType":"M","externalLoginType":"1","key":"电脑写字板","datatype":"1","page":"1","pagesize":"10","ext_attr":"no","brand_col":"no","price_col":"no","color_col":"no","size_col":"no","ext_attr_sort":"no","merge_sku":"yes","multi_suppliers":"yes","area_ids":"1,72,55674","filt_type":"redisstore,1;","qp_disable":"no","debug":"false","t1":"1752227822138","sourceType":"H5_home_page_search"}',
    "loginType": "2",
    "x-api-eid-token": "jdd03GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBEAAAAMX7C2NSPQAAAAAC7YKOXOEBJ37QAX",
    "h5st": "20250711175922805;wxazw3p3gwahjw35;ffb96;tk03wa0c61c4518n3CD5Y6piNlujVmw31gGU4GkctPMWwoAr5Gew9JhEw3GJ3iyyN9rn7l6-ec5njtoZiOSSCF1zfZtC;13ca3f0fa0461867d60059c5af0e4475;5.1;1752227956805;smePkmch7SHUGp4RHhrS7SHQNN3RMuMgMuHVMusmk_Mh9uMgM24WLlsmOGujMSLW_KbWLNLh5irgJtbg6qoi_SbVJNLh4iYWKd4h8ebWMuMgMuHdCRIWJRHmOuMsCmMiMdLV3OLW3urg1Wog8iYgKhbgLpbh6qbgKlLW1abiNlsm0m8SNVHTNhImOuMsCmciBmsm0msh5lImOuMsCmsgAqLj5W3XJ9YUIxZhGlsm0mMRMusmk_Mm8qIi7a5diRITMuMgM64TK1YW8lsmOGujMm7iAJ4ZMuMgMWoSMusmk_cPOuMs8uMgMqbi5lImOusmOGuj8qrm0msi9aHWMusmOuMsCObjOGLm8qbRMlsmOusmk_MmlJLV_lLU-RJaCBImOGLmBxoVApISMusmOuMsCurm0msg5lImOusmOGuj_uMgMSbRMlsmOusmk_sg7uMgMWbRMlsmOusmk_siOGLm5aHWMusmOuMsCurm0msh5lImOusmOGuj5erm0m8i5lImOusmOGujMaLj92siMuMgMqbRMlsmOusmk_siOGLmDRHmOusmOGuj96sm0m8SClsmOusmk_siOGLmClsmOusmk_siOGLmKRHmOusmOG_QOGLmK1YV6NXVMusmk_cPOuMsMiIV2argNR7i_iYWJR7XKFImOGLm8uHmOusmOGujMiIV2argNR7i_iYWJR7XKFImOGLm9uHmOusmOG_QOGLm_tHmOuMsCmMb2abec56jK9YU5lqm5u8S5NXVgt8g_2MhOeYU-lnVApqmzOXRAJobMuMgMqYR7lsmOG_Q;3d19e9d58d7c001e33d3eba910e1d7de;ri_uKJKT-JoRL1YRI9cQKxIWCeYU_tXW",
}
h5st = generate_h5st(params)
print(h5st)
print(len(h5st))
params["h5st"] = h5st
response = requests.get(url, headers=headers, cookies=cookies, params=params)

print(response.text)
print(response)
